﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UniverClient.ServiceReference1;

namespace UniverClient
{
    public partial class Edit : Form
    {
        List<Control> controls;
        UObjectFullData mainObject;
        public Edit()
        {
            InitializeComponent();
        }
        public Edit(UObjectFullData _object)
        {
            InitializeComponent();
            mainObject = _object;
            CreateForm();
        }

        private void CreateForm()
        {
            this.Text = mainObject.ClassName + " Edit";
            int i = 35;
            foreach (UObjectPropertie _field in mainObject.Fields)
            {
                // add object label
                Label _lb = new Label();
                _lb.Location = new System.Drawing.Point(20, i);
                _lb.Text = _field.FriendlyFieldName;
                this.Controls.Add(_lb);

                switch (_field.FieldType)
                {
                    case 4: 
                        #region create checkbox

                        CheckBox _ck = new CheckBox();
                        _ck.Location = new System.Drawing.Point(180, i);
                        //_ck.Text = _field.FieldData;
                        if (_field.FieldData != null ) _ck.Checked = bool.Parse(_field.FieldData);
                        this.Controls.Add(_ck);
                        controls.Add(_ck);
                        break;

                        #endregion

                    case 3:
                        #region create datatime

                        DateTimePicker _dt = new DateTimePicker();
                        _dt.Location = new System.Drawing.Point(180, i);
                        //_ck.Text = _field.FieldData;
                        if (_field.FieldData != null) _dt.Value = DateTime.Parse(_field.FieldData);
                        this.Controls.Add(_dt);
                        controls.Add(_dt);
                        break;

                        #endregion
                        
                    default:
                        #region create textbox

                        TextBox _tb = new TextBox();
                        _tb.Location = new System.Drawing.Point(180, i);
                        _tb.Size = new System.Drawing.Size(175, 20);
                        if (_field.FieldData != null)  _tb.Text = _field.FieldData;
                        this.Controls.Add(_tb);
                        controls.Add(_tb);
                        break;

                        #endregion                  
                }
                
                

                i += 40;
            }
            button1.Location = new Point(60, i);
            button2.Location = new Point(220, i);
            this.Size = new Size(400, 100 + i);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < mainObject.Fields.Length; i ++ )
            {
                switch (mainObject.Fields[i].FieldType)
                {
                    case 4:
                        #region create checkbox
                        mainObject.Fields[i].FieldData = ((CheckBox)controls[i]).Checked.ToString();
                        break;

                        #endregion

                    case 3:
                        #region create datatime
                        mainObject.Fields[i].FieldData = ((DateTimePicker)controls[i]).Value.ToShortDateString();
                        break;

                        #endregion

                    default:
                        #region create textbox
                        mainObject.Fields[i].FieldData = ((TextBox)controls[i]).Text;
                        break;
                        #endregion
                }
            }

            Close();
        }
    }
}
